package com.yz.jdd.module.bind.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yz.jdd.R;
import com.yz.jdd.base.activity.BaseActivity;

import com.yz.jdd.module.bind.presenter.BindPresenter;
import com.yz.jdd.module.main.MainActivity;

import com.yz.jdd.widget.TimerButton;

import butterknife.ButterKnife;
import butterknife.OnClick;

public class BindAccountActivity extends BaseActivity implements BindView {

    @butterknife.BindView(R.id.top_back)
    ImageView topBack;
    @butterknife.BindView(R.id.top_title)
    TextView topTitle;
    @butterknife.BindView(R.id.input_phone)
    EditText inputPhone;
    @butterknife.BindView(R.id.input_code)
    EditText inputCode;
    @butterknife.BindView(R.id.verify_code)
    TimerButton verify_code;

    @butterknife.BindView(R.id.bind)
    Button bind;

    private String aliUserId;

    private BindPresenter bindPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_account);
        ButterKnife.bind(this);
        initView();

        aliUserId = getIntent().getStringExtra("aliUserId");
        bindPresenter = new BindPresenter(this);
        bindPresenter.setAliUserId(aliUserId);
        bind.setEnabled(false);
    }

    private void initView() {
        topTitle.setText("绑定账号");
        topBack.setVisibility(View.VISIBLE);
        inputPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String valuePhone = inputPhone.getText().toString();
                String valueCode = inputCode.getText().toString();
                if (valuePhone.length() >= 1 && valueCode.length() >= 1) {
                    bind.setEnabled(true);
                } else {
                    bind.setEnabled(false);
                }
            }
        });

        inputCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String valuePhone = inputPhone.getText().toString();
                String valueCode = inputCode.getText().toString();
                if (valuePhone.length() >= 1 && valueCode.length() >= 1) {
                    bind.setEnabled(true);
                } else {
                    bind.setEnabled(false);
                }
            }
        });
    }

    @OnClick({R.id.top_back, R.id.bind, R.id.verify_code})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.top_back:
                finish();
                break;
            case R.id.bind:
                bindPresenter.doBindAccount();
                break;
            case R.id.verify_code:
                bindPresenter.doGetVerifyCode();
                break;
        }
    }

    @Override
    public String getPhoneNum() {
        return inputPhone.getText().toString();
    }

    @Override
    public String getVerifyCode() {
        return inputCode.getText().toString();
    }

    @Override
    public void startTimer() {
        verify_code.startTimer();
    }

    @Override
    public void disableVerifyCode() {
        verify_code.setEnabled(false);
    }

    @Override
    public void enableVerifyCode() {
        verify_code.setEnabled(true);
    }

    @Override
    public void setH5WebActivity() {

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

    @Override
    public void close() {
        finish();
    }
}
